package com.consumer1.mq;


import cn.hutool.json.JSONUtil;
import com.consumer1.annotations.UploadProgressAnnotation;
import com.consumer1.entity.ImportMovie;
import com.consumer1.util.FileUtils;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class ReceiveHandler {

    @Autowired
    private FileUtils fileUtils;

    private static final Logger LOG = LoggerFactory.getLogger(ReceiveHandler.class.getName());
   //监听队列
    @RabbitListener(queues = {RabbitmqConfig.QUEUE_TRANSFER_MOVIE})
    @UploadProgressAnnotation
    public void receive_transfer_movie(Message message, Channel channel){
        try {
            ImportMovie importMovie = JSONUtil.toBean(new String(message.getBody()), ImportMovie.class);
            fileUtils.transferM3U8(importMovie);
        } catch (Exception e) {
            LOG.error("消费者1号发生了异常: {}", e);
        }
    }
}
